/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import comu.Configuracio;
import controlador.autogenerats.ProductorJpaController;
import entity.Producte;
import entity.Productor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author David
 */
public class ProductorControlador extends ProductorJpaController{

    public ProductorControlador() {
        super(Persistence.createEntityManagerFactory(Configuracio.PERSISTANCE_UNIT));
    }
    
    public ArrayList<Productor> findProductorEntitiesByProducte(Producte producte){
        EntityManager em = getEntityManager();
        try{
        Query q = em.createNativeQuery("select * from productor where `id` = any (select camp.`productor_id` from camp where id = any "
                + "(Select produccio.`camp_id` from produccio where produccio.`productes_id` like "+ producte.getId() +")) order by `nom`", Productor.class);
        Iterator it = q.getResultList().iterator();
        ArrayList<Productor> llista = new ArrayList<Productor>();
        while(it.hasNext()){
            llista.add((Productor) it.next());
        }
        return llista;
        } finally {
            em.close();
        }
    }

    public ArrayList<Productor> findProductorEntitiesAZ() {
        EntityManager em = getEntityManager();
        try{
        Query q = em.createNativeQuery("select * from productor order by nom", Productor.class);
        Iterator it = q.getResultList().iterator();
        ArrayList<Productor> llista = new ArrayList<Productor>();
        while(it.hasNext()){
            llista.add((Productor) it.next());
        }
        return llista;
        } finally {
            em.close();
        }
    }
}
